package com.net.tkc.pro.controller;

import com.net.tkc.pro.entity.Post;
import com.net.tkc.pro.entity.ResultEntity;
import com.net.tkc.pro.entity.Subject;
import com.net.tkc.pro.impl.ShareImpl;
import com.net.tkc.pro.impl.SubjectImpl;
import com.net.tkc.pro.tool.JsonUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("/Subject/")
public class SubjectController {
    ResultEntity resultEntity = null;
    @Resource
    SubjectImpl subjectImpl;

    @GetMapping("Subject2")
    private String getList2(
    ){
        resultEntity = new ResultEntity();
        try{
            List<Subject> list =subjectImpl.getList2();
            if (list.size()!=0){
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            }else{
                resultEntity.setMsg("获取失败,请查看是否有数据");
                resultEntity.setCode("2");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 根据试卷查看题目
     * @param category
     * @return
     */
    @GetMapping("Subject")
    private String getList(
            @RequestParam("category") int category
    ){
        resultEntity = new ResultEntity();
        try{
            List<Subject> list =subjectImpl.getList(category);
            if (list.size()!=0){
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            }else{
                resultEntity.setMsg("获取失败,请查看是否有数据");
                resultEntity.setCode("2");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
    /**
     * 更新题库使用
     * @param examine 状态
     * @param id 题目id
     * @return
     */
    @PutMapping("Subject")
    private String upSubject(
            @RequestParam("examine") int examine,
            @RequestParam("id") int id
    ){
        resultEntity = new ResultEntity();
        try{
            int result = subjectImpl.upSubject(examine,id);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("修改成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("修改失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 添加题库
     * @param category 0卷一，1卷二
     * @param type 类型
     * @param Ttext 内容
     * @param choice1 选项
     * @param choice2
     * @param choice3
     * @param choice4
     * @param correct 答案
     * @return
     */
    @PostMapping("Subject")
    private String addSubject(
            @RequestParam("category") int category,
            @RequestParam("type") String type,
            @RequestParam("Ttext") String Ttext,
            @RequestParam("choice1") String choice1,
            @RequestParam("choice2") String choice2,
            @RequestParam("choice3") String choice3,
            @RequestParam("choice4") String choice4,
            @RequestParam("correct") String correct
    ){
        resultEntity = new ResultEntity();
        Subject subject=new Subject();
        subject.setCategory(category);
        subject.setType(type);
        subject.setTtext(Ttext);
        subject.setChoice1(choice1);
        subject.setChoice2(choice2);
        subject.setChoice3(choice3);
        subject.setChoice4(choice4);
        subject.setCorrect(correct);
        try{
            int result = subjectImpl.addSubject(subject);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("添加成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("添加失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
}
